{% extends "mobile/portal_template.html" %}
{% load basiccomparisonfilters %}

{% block script %}

{% include "tardis_portal/javascript_libraries.html" %}

<script type="text/javascript">
	$(document).ready(function(e) {


		$('.filterform').live('submit', function(e) {
				e.preventDefault();

				filterbox = $(this).find('.filename_search');

				dataset_id = filterbox.siblings('.dataset_id').val();

				filterbox.parents('.datafile_list').load("/ajax/datafile_list/" + dataset_id + "/?filename=" + filterbox.val(), function(){
						$(document).ready(function(){
								// datafile metadata
								$(".datafile_info_toggle").unbind('click');
								$(document).find('.datafile_info_toggle').text("Show Metadata");
								$(document).find('.datafile_info').hide();
								 $(document).find('.datafile_info_toggle').toggle(function(){
					 				$(this).text("Hide Metadata");
									var href = $(this).attr("href");
									$(this).next().show();

									//if($(this).next().text().replace(/^\s+|\s+$/g,'') == "")
									//{
										$(this).next().html(loadingHTML);
									//}
									$(this).next().load(href);

								 },function(){
									$(this).text("Show Metadata");
									$(this).next().hide();
								 });
							});

						});


			});


		// show hide various metadata divs

	   $('#datasets').find('.dataset_info').end().find('h2').click(function() {
	     $(this).next().toggle();
	   });

	   $('.dataset_info').find('.datafile_list').hide().end().find('.datafile_list_toggle').click(function(e) {
		e.preventDefault();
	 	$(this).next().toggle();

	   });


		$('.pagelink').live('click', function( event ){
					var href = $(this).attr("href");
					html = $(this).html();
					$(this).html(html + " " + loadingHTML);
					$(this).parents('.datafile_list').load(href, function(){
							$(document).ready(function(){
									// datafile metadata
									$(".datafile_info_toggle").unbind('click');
									$(document).find('.datafile_info_toggle').text("Show Metadata");
									$(document).find('.datafile_info').hide();
									 $(document).find('.datafile_info_toggle').toggle(function(){
						 				$(this).text("Hide Metadata");
										var href = $(this).attr("href");
										$(this).next().show();

										//if($(this).next().text().replace(/^\s+|\s+$/g,'') == "")
										//{
											$(this).next().html(loadingHTML);
										//}
										$(this).next().load(href);

									 },function(){
										$(this).text("Show Metadata");
										$(this).next().hide();
									 });

									$(".dataset_selector_all").unbind('click');
									$(".dataset_selector_none").unbind('click');
									// file selectors
									$(document).find('.dataset_selector_all').click(function() {
										$(this).parent().find('.datafile_checkbox').attr("checked", "checked");

								   });

									$(document).find('.dataset_selector_none').click(function() {
										$(this).parent().find('.datafile_checkbox').removeAttr("checked");

								   });
								});

					//$(this).html(html);

				})
				return false;
			});

	// default text set

	$(".dataset_metadata_toggle").text("Show Dataset Metadata");

	var loadingHTML = "<img src='/site_media/images/ajax-loader.gif'/>";

	// metadata text / ajax toggle

	// dataset md
	 $(".dataset_metadata_toggle").toggle(function(){
	   $(this).text("Hide Dataset Metadata");
	    var href= $(this).attr("href");
		$(href).show();

	 },function(){
	   $(this).text("Show Dataset Metadata");
	    var href= $(this).attr("href");
		$(href).hide();
	 });

	// datafile list
	 $(".datafile_list_toggle").toggle(function(e){
	   $(this).text("Hide Data Files");
		var href = $(this).attr("href");


		$(this).next().html(loadingHTML + "<br/>");

		var datafile_info_container = $(this).next();

		// add initialisation for new datafile handlers as callback once ajax has loaded
		// damn I love jQuery
		$(this).next().load(href,function(e){

				$(document).ready(function(e){

					var datafile_info_selectors =
						datafile_info_container.find('.datafile_info').hide().end().find('.datafile_info_toggle');

					datafile_info_selectors.text("Show Metadata");

					// datafile metadata
					 datafile_info_selectors.toggle(function(){
		 				$(this).text("Hide Metadata");
						var href = $(this).attr("href");

						//if($(this).next().text().replace(/^\s+|\s+$/g,'') == "")
						//{
							$(this).next().html(loadingHTML);
						//}
						$(this).next().load(href);

					 },function(){
						$(this).text("Show Metadata");
					 });

					   datafile_info_selectors.click(function() {
						$(this).next().toggle();
					   });

						// file selectors
						datafile_info_container.find('.dataset_selector_all').click(function() {
							$(this).parent().find('.datafile_checkbox').attr("checked", "checked");

					   });

						datafile_info_container.find('.dataset_selector_none').click(function() {
							$(this).parent().find('.datafile_checkbox').removeAttr("checked");

					   });

				});

			 });

	 },function(e){
	   $(this).text("Show Data Files");

	 });

	});
</script>

{% endblock %}

{% block content %}

<div class="post">
	<span class="graytitle">{{experiment.title}}</span>
	<ul class="pageitem">

			<li class="textbox">

					<div class="demo">

					<div id="tabs">

						<div id="tabs-1">

                            <p>
                            <span class="header">Authors</span>
                            {% for author in authors %}
                                    {{ author.author.name }}{% if not forloop.last %},{% endif %}
                            {% endfor %}
                            </p>

							<p>
							<span class="header">Abstract</span>
							<div class="abstract">
							{{ experiment.description|safe }}
							</div>
							</p>

							{% if experiment.handle %}
							<p>
							<strong>Persistent Handle:</strong>
							<a href="{{ handle }}/{{ experiment.handle }}">{{ experiment.handle }}</a><br/>
							</p>
							{% endif %}

							<p>
							<span class="header">Institution</span>
							{{experiment.institution_name}}<br/>
							</p>

							<p>
								<div class="dataset_extrainfo">
									{% if experiment.experimentparameterset_set.count %}
									<strong>Experiment Metadata: </strong><a class="dataset_metadata_toggle" href="#dataset_metadata_{{forloop.counter}}">[Hide]</a>
									<div class="dataset_metadata" id="dataset_metadata_{{forloop.counter}}">
									<ul>
											{% for parameterset in experiment.experimentparameterset_set.all %}
											<p><strong><em>{{parameterset.schema}}</strong></em></p>

											{% for parameter in parameterset.experimentparameter_set.all %}
												<li><strong>{{ parameter.name.full_name }}: </strong>
												  {{ parameter.get }}
												</li>
											{% endfor %}
											<br/><br/>
											{% endfor %}
									</ul>
									</div>
									</div>
									{% endif %}
							</p>

							<p>
								<span class="header">Dataset Information</span>
								<div>
									<!-- <strong>Dataset Metadata: </strong>	 -->
									<div class="dataset_information">
										<ul>
												<li><strong>Datasets:</strong> {{experiment.dataset_set.all.count}}</li>
												<li><strong>Files:</strong> {{datafiles.count}}</li>
												<li><strong>Size:</strong> {{size|filesizeformat}}</li>
										</ul>
									</div>
								</div>
							</p>

							<p>
							<span class="header">Experiment Last Updated</span>
							{{ experiment.update_time|date:"jS F Y H:i" }}<br/>
							</p>

                            <p align="right">

							{% if owners %}
							<br/>
							<span class="header">Experiment Administrators</span>
                            {% for owner in owners %}
                                    {{ owner.username }} {% if owner.email %}(<a href="mailto:{{owner.email}}">{{ owner.email}}</a>{% endif %}){% if not forloop.last %}<br/>{% endif %}
                            {% endfor %}
							{% endif %}
                            </p>

						</div>
						<div id="tabs-2">
							<br/>

							<input type="hidden" name="expid" value="{{experiment.id}}">
							<div id="datasets">
							{% for dataset in experiment.dataset_set.all %}
								<span class="header" style="color: black;">
									<strong>Dataset {{ forloop.counter }}</strong>
								</span>

								<div class="dataset_info">

									<div class="dataset_description">
									<span class="header">Dataset Description</span>{{ dataset.description|safe }}
									</div>
									<br/>

									<div class="dataset_extrainfo">
										{% if dataset.datasetparameterset_set.count %}
										<strong>Dataset Metadata: </strong><a class="dataset_metadata_toggle" href="#dataset_metadata_{{forloop.counter}}">[Hide]</a>
										<div class="dataset_metadata" id="dataset_metadata_{{forloop.counter}}">
										<ul>
												{% for parameterset in dataset.datasetparameterset_set.all %}
												<p><strong><em>{{parameterset.schema}}</strong></em></p>

												{% for parameter in parameterset.datasetparameter_set.all %}
													<li><strong>{{ parameter.name.full_name }}: </strong>
													  {{ parameter.get }}
													</li>
												{% endfor %}
												<br/><br/>
												{% endfor %}
										</ul>
										</div>
										{% endif %}

										<div>
											<a class="button datafile_list_toggle" href="/ajax/datafile_list/{{ dataset.id }}">Show Data Files</a>
											<ul class="datafile_list">

											</ul>

										</div>
									</div>
								</div>
							{% endfor %}
							</div>

			</li>

	</ul>
</div>

{% endblock %}
